home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / src.zoo / src / getshell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-17  |  5.2 KB  |  257 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: getshell.c,v 1.1 89/03/17 08:21:11 sau Exp $
  9.     $Source: /m1/mgr.new/src/RCS/getshell.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /m1/mgr.new/src/RCS/getshell.c,v $$Revision: 1.1 $";
  12.  
  13. /* start a shell */
  14.  
  15. #include <sys/file.h>
  16. #include <sys/signal.h>
  17. #include <sgtty.h>
  18. #include <stdio.h>
  19. #include "bitmap.h"
  20. #include "defs.h"
  21.  
  22. #define SHELL        "/bin/sh"
  23.  
  24. static char line[] = {"/dev/ptypX"};
  25. static int  pty_index=5;        /* better hit rate than 0 */
  26. extern char **environ;
  27.  
  28. /*    get a pty line */
  29.  
  30. int
  31. getapty()
  32.    {
  33.    register int i;
  34.    int fd;
  35.  
  36.    line[5] = 'p';
  37.    for(line[8]='p';line[8]<'r';line[8]+= 1)
  38.       for (i=1;i<=16;i++) {
  39.          line[9]="0123456789abcdef"[(pty_index+i)%16];
  40.          if ((fd = open(line,2)) >= 0) {
  41.             /* pty_index = (pty_index+i)%16;   temp */
  42.             line[5] = 't';
  43.             return(fd);
  44.             }
  45.          }
  46.    return(-1);
  47.    }
  48.       
  49. int getatty()
  50.    {
  51.    int fd;
  52.    line[5]='t';
  53.    fd=open(line,2);
  54.    if (fd<0) {
  55.       sleep(3);
  56.       return (open(line,2));
  57.       }
  58.    return(fd);
  59.    }
  60.  
  61. char *
  62. last_tty()
  63.    {
  64.    return(line);
  65.    }
  66.  
  67. /******************************************************************************/
  68. /* start a command */
  69.  
  70. get_command(argv,file)
  71. char **argv;
  72. int *file;
  73.    {
  74.    register int i;                /* counter */
  75.    int fd;                    /* file desc */
  76.    int tty;                    /* fd of /dev/tty */
  77.    int pid;                    /* pid of shell */
  78.    int group;                    /* process group id */
  79.    int tty_slots;                /* # of tty slots */
  80.    char *name, *get_path();
  81.    char *getenv();
  82.    char *shell = getenv("SHELL");
  83.    char *arg[2];
  84.    char who[MAXNAME];
  85.  
  86.    if (argv == (char **) 0 ) {
  87.       argv = arg;
  88.       *argv = shell?shell:SHELL;
  89.       *(argv+1) = (char *) 0;
  90.       }
  91.    name = get_path(argv[0]);
  92.  
  93.    if (name == (char *) 0 || *name == '\0')
  94.       return(-2);
  95.  
  96. #ifdef DEBUG
  97.    dprintf(s)(stderr,"EXECING: ");
  98.    for(i=0;argv[i]!='\0';i++)
  99.       dprintf(s)(stderr,"%s ",argv[i]);
  100.    dprintf(s)(stderr,"\n");
  101. #endif
  102.  
  103.    if ((*file=getapty()) < 0)
  104.       return(-1);
  105.  
  106.    if ((pid=fork()) > 0) {
  107.       /* parent side of fork */
  108.       char buff[2];
  109.       read(*file,buff,sizeof(buff));    /* wait for slave side to open */
  110. #ifdef DEBUG
  111.       dprintf(s)(stderr,"EXEC done, slave side open\r\n ");
  112. #endif
  113.       return(pid);
  114.       }
  115.    else if (pid<0)
  116.       /* error side of fork */
  117.       return(pid);
  118.  
  119.    /* child side of fork */
  120.    for(i=0;i<NSIG;i++)
  121.       signal( i, SIG_DFL );
  122.  
  123.    /* void association with controlling terminal */
  124.  
  125. #ifdef TIOCNOTTY
  126.    tty = open("/dev/tty",0);
  127.    ioctl(tty,TIOCNOTTY,0);
  128.    close(tty);
  129. #endif
  130.  
  131.    /* open slave side of ptty */
  132.  
  133.    if ((fd=getatty())<0) {
  134.       _quit("");
  135.       perror("Slave side of p-tty won't open");
  136.       exit(1);
  137.       }
  138.  
  139.    group=getpid();
  140.  
  141. #ifndef SYSV
  142.    tty_slots = getdtablesize();
  143. #else
  144.    tty_slots = 20;
  145. #endif
  146.  
  147.    for(i=0;i<tty_slots;i++) if (i != fd) close(i);
  148.  
  149.    /* set the uid-0 stuff up */
  150.  
  151.    if (geteuid() < 1) {
  152.       int uid = getuid();
  153.       fchmod(fd,0622);
  154.       fchown(fd,uid,-1);
  155.       setreuid(uid,uid);
  156.  
  157.       uid = getgid();
  158.       fchown(fd,-1,uid);
  159.       setregid(uid,uid);
  160.       }
  161.  
  162.    i = dup(fd);
  163.    close(fd);
  164.    dup(i);
  165.    dup(i);
  166.  
  167.    setpgrp(group,group);
  168.    ioctl(0,TIOCSPGRP,&group);
  169.  
  170.     adjust_mode(NTTYDISC,ECHO|CRMOD|EVENP|ODDP);
  171.     restore_modes(0);
  172.  
  173.    /* add a utmp entry */
  174.  
  175. #ifdef WHO
  176.    add_utmp(0,sprintf(who,"%s%c",HOST,line[9]));
  177. #endif
  178.  
  179.    /* start the command */
  180.  
  181. #ifdef DEBUG
  182.    dprintf(s)(stderr,"execing %s (%s ...)\r\n",name,*argv);
  183. #endif
  184.  
  185.    do_env("TERM=",TERMNAME);
  186.    do_env("TERMCAP=","");
  187.  
  188.    write(2,"\n",1);    /* tell master that slave side is open */
  189.    execve(name,argv,environ);
  190.    _exit(1);
  191.    }
  192.  
  193. /* half open a ptty then return */
  194.  
  195. char *
  196. half_open(file)
  197. int *file;
  198.    {
  199.    register int i;                /* counter */
  200.    int pid;                    /* file desc */
  201.  
  202.    if ((*file=getapty()) < 0)
  203.       return((char *) 0);
  204.    ioctl(*file,TIOCREMOTE,0);    /* I dunno */
  205.    return(line);
  206.    }
  207.  
  208. /* get a complete path name from command */
  209.  
  210. static char path[512];
  211. static char start[512];
  212.  
  213. char *
  214. get_path(name)
  215. char *name;
  216.    {
  217.    char *getenv(), *index();
  218.    register char c, *next, *list;
  219.  
  220.    if (index("/.",*name))
  221.       if (access(name,X_OK)==0)
  222.          return(name);
  223.       else
  224.          return((char *)0);
  225.  
  226.    strcpy(start,getenv("PATH"));
  227.    for(list=start;next=index(list,':');list=next+1) {
  228.       *next = '\0';
  229.       sprintf(path,"%s/%s",list,name);
  230.       if (access(path,X_OK) == 0)
  231.          return(path);
  232.       }
  233.  
  234.    sprintf(path,"%s/%s",list,name);
  235.    if (list && access(path,X_OK) == 0) {
  236.       return(path);
  237.       }
  238.    else {
  239.       return((char *) 0);
  240.       }
  241.    }
  242.  
  243. /* change an environment variable */
  244.  
  245. do_env(name,value)
  246. char *name, *value;
  247.    {
  248.    register int i;
  249.    int n = strlen(name);
  250.    
  251.    for(i=0;environ[i] != (char *) 0;i++)
  252.       if (strncmp(environ[i],name,n) == 0) {
  253.          strcpy(environ[i]+n,value);
  254.          break;
  255.          }
  256.    }
  257.